package web.suzy.oj.config;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import web.suzy.oj.manager.admin.system.ConfigManager;
import web.suzy.oj.pojo.vo.ConfigVO;

import java.util.Objects;

/**
 * YangSuzy 软件工作室
 * 类名: StartupRunner
 * 描述: ---- 待定 -----
 * 功能: 项目启动后初始化执行此类
 * ==========未完成=============
 *
 * @author YangSuzy
 * Date: 2022/11/17 18:00
 */
@Component
@Slf4j(topic = "suzyoj")
public class StartupRunner implements CommandLineRunner {

    @Autowired
    private ConfigVO configVo;

    @Autowired
    private ConfigManager configManager;

    @Autowired
    private NacosSwitchConfig nacosSwitchConfig;

    /**
     * jwt配置
     */
    @Value("${jwt-token-secret}")
    private String tokenSecret;

    @Value("${jwt-token-expire}")
    private String tokenExpire;

    @Value("${jwt-token-fresh-expire}")
    private String checkRefreshExpire;

    /**
     * 数据库配置
     */
    @Value("${mysql-username}")
    private String mysqlUsername;

    @Value("${mysql-password}")
    private String mysqlPassword;

    @Value("${mysql-name}")
    private String mysqlDBName;

    @Value("${mysql-host}")
    private String mysqlHost;

    @Value("${mysql-public-host}")
    private String mysqlPublicHost;

    @Value("${mysql-port}")
    private Integer mysqlPort;

    @Value("${mysql-public-port}")
    private Integer mysqlPublicPort;

    /**
     * 缓存配置
     */
    @Value("${redis-host}")
    private String redisHost;

    @Value("${redis-port}")
    private Integer redisPort;

    @Value("${redis-password}")
    private String redisPassword;

    /**
     * 判题服务token
     */
    @Value("${judge-token}")
    private String judgeToken;

    /**
     * 邮箱配置
     */
    @Value("${email-username}")
    private String emailUsername;

    @Value("${email-password}")
    private String emailPassword;

    @Value("${email-host}")
    private String emailHost;

    @Value("${email-port}")
    private Integer emailPort;

    /**
     * 方法名: run
     * 描述: 项目启动后优先执行此方法
     *
     * @param args
     * @return void
     * @date 2022/11/17 18:04
     * @auther YangSuzy
     **/
    @Override
    public void run(String... args) throws Exception {
        //修改nacos默认配置文件
        initDefaultConfig();
        //修改nacos网站基本配置文件
        initWebConfig();
        //修改nacos功能开关配置文件
        initSwitchConfig();
    }

    /**
     * 方法名: initDefaultConfig
     * 描述: 初始化默认配置
     *
     * @return void
     * @date 2022/11/17 18:29
     * @auther YangSuzy
     **/
    private void initDefaultConfig() {
        //设置评测服务token
        if (judgeToken.equals("default")) {
            configVo.setJudgeToken(IdUtil.fastSimpleUUID());
        } else {
            configVo.setJudgeToken(judgeToken);
        }
        //设置JWT私钥
        if (tokenSecret.equals("default")) {
            if (StrUtil.isBlank(configVo.getTokenSecret())) {
                configVo.setTokenSecret(IdUtil.fastSimpleUUID());
            }
        } else {
            configVo.setTokenSecret(tokenSecret);
        }
        configVo.setTokenExpire(tokenExpire);
        configVo.setCheckRefreshExpire(checkRefreshExpire);
        //设置MySql
        configVo.setMysqlUsername(mysqlUsername);
        configVo.setMysqlPassword(mysqlPassword);
        configVo.setMysqlHost(mysqlHost);
        configVo.setMysqlPublicHost(mysqlPublicHost);
        configVo.setMysqlPort(mysqlPort);
        configVo.setMysqlPublicPort(mysqlPublicPort);
        configVo.setMysqlDBName(mysqlDBName);
        //设置Redis
        configVo.setRedisHost(redisHost);
        configVo.setRedisPort(redisPort);
        configVo.setRedisPassword(redisPassword);
        //发送配置
        configManager.sendNewConfigToNacos();
    }

    /**
     * 方法名: initWebConfig
     * 描述: 初始化网站基本配置
     *
     * @return void
     * @date 2022/11/17 18:29
     * @auther YangSuzy
     **/
    private void initWebConfig() {
        WebConfig webConfig = nacosSwitchConfig.getWebConfig();
        boolean isChanged = false;
        if (!Objects.equals(webConfig.getEmailHost(), emailHost)
                && (webConfig.getEmailHost() == null || !"your_email_host".equals(emailHost))) {
            webConfig.setEmailHost(emailHost);
            isChanged = true;
        }
        if (!Objects.equals(webConfig.getEmailPort(), emailPort)
                && (webConfig.getEmailPort() == null || emailPort != 456)) {
            webConfig.setEmailPort(emailPort);
            isChanged = true;
        }
        if (!Objects.equals(webConfig.getEmailUsername(), emailUsername)
                && (webConfig.getEmailUsername() == null || !"your_email_username".equals(emailUsername))) {
            webConfig.setEmailUsername(emailUsername);
            isChanged = true;
        }
        if (!Objects.equals(webConfig.getEmailPassword(), emailPassword)
                && (webConfig.getEmailPassword() == null || !"your_email_password".equals(emailPassword))) {
            webConfig.setEmailPassword(emailPassword);
            isChanged = true;
        }
        if (isChanged) {
            nacosSwitchConfig.publishWebConfig();
        }
    }

    /**
     * 方法名: initSwitchConfig
     * 描述: 初始化功能开关配置
     *
     * @return void
     * @date 2022/11/17 18:31
     * @auther YangSuzy
     **/
    private void initSwitchConfig() {
        SwitchConfig switchConfig = nacosSwitchConfig.getSwitchConfig();
        boolean isChanged = false;
        if (isChanged) {
            nacosSwitchConfig.publishWebConfig();
        }
    }

}
